package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.CustomerQueryObject;
import cn.wolfcode.qo.JSONResult;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IEmployeeService employeeService;

    @RequiresPermissions(value = {"customer:potentialList", "客户列表"}, logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String list(Model model, @ModelAttribute("qo") CustomerQueryObject qo) {
        // 判断subject时候包含admin或者Market_Manager, 如果有, 可以查询所有的数据
        Subject subject = SecurityUtils.getSubject();
        System.out.println(subject.hasRole("admin"));
        if (!(subject.hasRole("admin") || subject.hasRole("Market_Manager"))) {
            qo.setSellerId(UserContext.getCurrentUser().getId());
        }

        // 只查询潜在用户状态的数据
        qo.setStatus(Customer.STATUS_COMMON);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        // 查询带有Market, Market_Manager角色的员工
        List<Employee> sellers = employeeService.selectByRoleSn("Market", "Market_Manager");
        model.addAttribute("sellers", sellers);

        // 查询职业下拉框的数据
        List<SystemDictionaryItem> jobs = customerService.selectTitleBySn("job");
        model.addAttribute("jobs", jobs);
        // 查询来源下拉框的数据
        List<SystemDictionaryItem> sources = customerService.selectTitleBySn("source");
        model.addAttribute("sources", sources);

        // 查询交流方式下拉框的数据
        List<SystemDictionaryItem> ccts = customerService.selectTitleBySn("communicationMethod");
        model.addAttribute("ccts", ccts);

        // 查询跟进结果下拉框的数据
        List<SystemDictionaryItem> scores = customerService.selectTitleBySn("score");
        model.addAttribute("scores", scores);

        return "customer/potentialList";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"customer:delete", "客户删除"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id) {
        if (id != null) {
            customerService.delete(id);
        }
        return new JSONResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate", "客户新增/修改"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult saveOrUpdate(Customer customer) {
        if (customer.getId() != null) {
            customerService.update(customer);
        } else {
            customerService.save(customer);
        }
        return new JSONResult();
    }
}
